package com.sinoglobal.sinostore.fragment;

import java.util.ArrayList;
import java.util.List;

import andbase.view.pullview.AbPullListView;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;

import com.sinoglobal.sinostore.R;
import com.sinoglobal.sinostore.adapter.CityAdapter;
import com.sinoglobal.sinostore.bean.CityVo;
import com.sinoglobal.sinostore.utils.ListViewUtils;

/**
 * 
 * @author lk
 * @createdate 2014-9-11 上午10:38:53
 * @Description: TODO(市级数据显示)
 */
public class CityFragment extends BaseFragment implements OnItemClickListener {
	private CityAdapter adapter; 
	private AddressInterface addressInterface;
	private AbPullListView cityListView; //城市列表
	private ImageView imageView;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		adapter = new CityAdapter(getActivity(), new ArrayList<CityVo>());

		// 获取所有市级数据
		Bundle bundle = getArguments();
		String city = bundle.getString("province");
		if (null != city) {
			getCity(city);
		}
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.shop_warecomment_view, null);
		// 初始化控件
		initView(view);
		cityListView.setAdapter(adapter);
		return view;
	}

	/**
	 * 
	 * @author lk
	 * @createdate 2014-9-11 上午9:59:10
	 * @Description: (初始化控件)
	 * @param view
	 * 
	 */
	private void initView(View view) {
		cityListView = (AbPullListView) view.findViewById(R.id.listView1);
		cityListView.setVisibility(View.VISIBLE);
		cityListView.setPullLoadEnable(false);
		cityListView.setPullRefreshEnable(false);
		cityListView.setOnItemClickListener(this);
		ListViewUtils.setListViewHeightBasedOnChildrenThree(cityListView);
	}

	@Override
	public void onAttach(Activity activity) {
		// TODO Auto-generated method stub
		super.onAttach(activity);
		if (activity instanceof AddressInterface) {
			addressInterface = (AddressInterface) activity;
		}
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View view, int position,
			long arg3) {
		CityVo cityVo = adapter.getItem(position - 1);
		ZoneFragment fragment = new ZoneFragment();
		Bundle bundle = new Bundle();
		bundle.putSerializable("city", cityVo);
		fragment.setArguments(bundle);
		addressInterface.setCity(cityVo, this, fragment);
	}

	/**
	 * 
	 * @author lk
	 * @createdate 2014-9-11 上午10:55:35
	 * @Description: (获取市级数据)
	 * @param proId
	 * 
	 */
	private void getCity(final String proId) {
		new AsyncTask<Void, Void, List<CityVo>>() {
			@Override
			protected List<CityVo> doInBackground(Void... arg0) {
				return sqLiteManager.getCity(proId);
			}

			@Override
			protected void onPostExecute(List<CityVo> result) {
				super.onPostExecute(result);
				if (null != result) {
					adapter.addList(result);
				}
			}

		}.execute();
	}
}
